<?php

namespace app\model\v1\recovery;


use app\model\user\User;
use constants\ProductConstants;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\Model;

class ConfigAbout extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_config_about';

    const VERSION_TYPE = 1;
    const MSG_TEXT_TYPE = 2;
    const AGREMENT_TYPE = 3;
    const PRIVACY_TYPE = 4;


    /**
     * 关于我们 - 检查更新
     * @param $param
     * @return int
     */
    public static function checkUpdateVersion($param = [])
    {
        $item = self::where("type", self::VERSION_TYPE)
            ->where("app_type", $param['app_type'])
            ->where("version", '>', $param['version'])
            ->where("type",1)
            ->where("status",1)
            ->where("delete_time",null)
            ->limit(1)
            ->order("id", "desc")
            ->find() ?? [];

        return $item;
    }


    /**
     * 关于我们 - 获取详情
     * @param $param
     * @return ConfigAbout|array|mixed|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAboutInfo($param = [])
    {
        $info = [];
        switch ($param['type']){
            case 1:
                $info = self::whereNull("delete_time")->where(["type"=>$param['type'],'status'=>1])->where("app_type", $param['app_type'])->order("id", 'desc')->limit(1)->find() ?? [];
                break;
            case 2:
                $info = self::whereNull("delete_time")->where(["type"=>$param['type'],'status'=>1])->where("app_type", $param['app_type'])->find() ?? [];
                break;
            case 3:
                $info = self::whereNull("delete_time")->where(["type"=>$param['type'],'status'=>1])->where("app_type", $param['app_type'])->find() ?? [];
                break;
            case 4:
                $info = self::whereNull("delete_time")->where(["type"=>$param['type'],'status'=>1])->where("app_type", $param['app_type'])->find() ?? [];
                break;
            case 5:
                $info = self::whereNull("delete_time")->where(["type"=>$param['type'],'status'=>1])->where("app_type", $param['app_type'])->find() ?? [];
                break;
        }

        return $info;
    }




    /**
     * 版本信息管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getConfigAboutList($param = [])
    {
        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
                when status=0 then '禁用' 
                end as status_text,
            case when type=1 then '版本' 
                when type=2 then '注销提醒文案' 
                when type=3 then '用户协议' 
                when type=4 then '隐私政策' 
                when type=5 then '会员服务协议' 
                when type=6 then '友盟系统消息标题' 
                end as type_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                $query->where("type", 1);
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),  strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("content",'like', '%'.$param["word"].'%');
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                $query->where("type", 1);
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),  strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("content",'like', '%'.$param["word"].'%');
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->count();

        return $data;
    }


    /**
     * 版本信息管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateConfigAbout($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["version"]))       $update["version"] = $param["version"];
        if(!empty($param["content"]))       $update["content"] = $param["content"];
        if(!empty($param["desc"]))          $update["desc"] = $param["desc"];
        if(!empty($param["num_text"]))      $update["num_text"] = $param["num_text"];
        if(!empty($param["icon"]))          $update["icon"] = $param["icon"];
        if(!empty($param["type"]))          $update["type"] = $param["type"];
        if(!empty($param["sort"]))          $update["sort"] = $param["sort"];
        if(!empty($param["android_link"]))  $update["android_link"] = $param["android_link"];
        if(!empty($param["ios_link"]))      $update["ios_link"] = $param["ios_link"];
        if(!empty($param["app_type"]))      $update["app_type"] = $param["app_type"];
        if(!empty($param["is_upload"]))     $update["is_upload"] = $param["is_upload"];
        if(!empty($param["is_show"]))       $update["is_show"] = $param["is_show"];

        return self::where("id", $param['id'])->update($update);
    }



    /**
     * 版本信息管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addConfigAbout($param = [])
    {
        $add = [
            "version"   => $param["version"] ?? '',
            "content"   => $param["content"] ?? '',
            "desc"      => $param["desc"] ?? '',
            "num_text"  => $param["num_text"] ?? '',
            "icon"      => $param["icon"] ?? '',
            "type"      => $param["type"] ?? '',
            "sort"      => $param["sort"] ?? '',
            "android_link" => $param["android_link"] ?? '',
            "ios_link"  => $param["ios_link"] ?? '',
            "app_type"  => $param["app_type"] ?? '',
            "is_upload" => $param["is_upload"] ?? '',
            "is_show"   => $param["is_show"] ?? '',
            "create_time"  => time(),
        ];
        return self::insert($add);
    }



    /**
     * 版本信息管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableConfigAbout($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }

}